package org.luxor.commons.zookeeper.client;

import org.apache.curator.framework.CuratorFramework;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.ACL;
import org.luxor.commons.core.utils.StrKit;
import org.luxor.commons.core.utils.StringUtils;
import org.luxor.commons.zookeeper.component.WatcherManager;
import org.luxor.commons.zookeeper.exception.ZKDataEncodingException;
import org.luxor.commons.zookeeper.exception.ZookeeperException;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;

/**
 * Zookeeper Client
 *
 * @author Mr.Yan  @date: 2020/12/21
 */
public class ZookeeperClient extends ZookeeperWatcherClient {

    public ZookeeperClient(CuratorFramework curatorFramework, WatcherManager watcherManager) {
        super(curatorFramework,watcherManager);
    }

    /**
     * 创建节点
     *
     * @param path       节点路径
     * @param createMode 节点模式
     * @param textData   文本类型的节点数据
     * @param aclList    节点访问权限，默认为"开放访问"
     */
    public void create(String path, CreateMode createMode, String textData, ArrayList<ACL> aclList) throws ZookeeperException {
        if (StringUtils.isBlank(textData)) {
            textData = StrKit.EMPTY;
        }
        try {
            byte[] data = textData.getBytes(StrKit.CHARSET_NAME);
            this.create(path, createMode, data, aclList);
        } catch (UnsupportedEncodingException e) {
            throw new ZKDataEncodingException("字符串解码失败", e);
        }
    }

    /**
     * 创建持久节点
     *
     * @param path     节点路径
     * @param textData 文本类型的节点数据
     */
    public void create(String path, String textData) throws ZookeeperException {
        if (StringUtils.isBlank(textData)) {
            textData = StrKit.EMPTY;
        }
        try {
            byte[] data = textData.getBytes(StrKit.CHARSET_NAME);
            this.create(path, data);
        } catch (UnsupportedEncodingException e) {
            throw new ZKDataEncodingException("字符串解码失败", e);
        }
    }

    /**
     * 创建临时节点
     *
     * @param path 节点路径
     * @param textData 文本类型的节点数据
     */
    public void createEphemera(String path, String textData) throws ZookeeperException {
        if (StringUtils.isBlank(textData)) {
            textData = StrKit.EMPTY;
        }
        try {
            byte[] data = textData.getBytes(StrKit.CHARSET_NAME);
            this.createEphemera(path, data);
        } catch (UnsupportedEncodingException e) {
            throw new ZKDataEncodingException("字符串解码失败", e);
        }
    }

    /**
     * 获取文本数据
     *
     * @param path 节点路径
     * @return 节点数据
     */
    public String getDataStr(String path) throws ZookeeperException {
        byte[] data = getData(path);
        if (data == null) {
            return null;
        }
        try {
            return new String(data, StrKit.CHARSET_NAME);
        } catch (UnsupportedEncodingException e) {
            throw new ZKDataEncodingException("字符串编码失败", e);
        }
    }

    /**
     * 修改数据
     *
     * @param path     节点路径
     * @param textData 文本类型的节点数据
     */
    public void setData(String path, String textData) throws ZookeeperException {
        if (StringUtils.isBlank(textData)) {
            textData = StrKit.EMPTY;
        }
        try {
            byte[] data = textData.getBytes(StrKit.CHARSET_NAME);
            setData(path, data);
        } catch (UnsupportedEncodingException e) {
            throw new ZKDataEncodingException("字符串解码失败", e);
        }
    }
}
